import math
from models.models import StoreGoods
from django.http.response import JsonResponse
from models.models.RequestResult import RequestResult as Res
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache


# 获取推荐商品
@csrf_exempt
def get_recommend_goods(request):
    recommend_goods = StoreGoods.objects.filter(is_recommend=1).order_by("-id")[0:6]
    res_recommend_goods = list()
    for g in recommend_goods:
        res_g = dict()
        res_g["id"] = g.id
        res_g["goods_title"] = g.goods_title
        res_g["goods_img"] = g.goods_img
        res_g["keyword"] = g.goods_keyword
        res_g["goods_price"] = \
            min(g.goods_wph_price, g.goods_jd_price, g.goods_tm_price, g.goods_tb_price, g.goods_sn_price)
        res_recommend_goods.append(res_g)
    return JsonResponse(Res.success(res_recommend_goods))


# 获取精选商品
@csrf_exempt
def get_selection_goods(request):
    selection_goods = StoreGoods.objects.filter(is_selection=1).order_by("-id")[0:6]
    res_selection_goods = list()
    goods_index = 0
    for g in selection_goods:
        res_g = dict()
        res_g["id"] = g.id
        res_g["goods_title"] = g.goods_title
        res_g["goods_img"] = g.goods_img
        res_g["goods_price"] = \
            min(g.goods_wph_price, g.goods_jd_price, g.goods_tm_price, g.goods_tb_price, g.goods_sn_price)
        res_g["index"] = goods_index
        goods_index += 1
        res_selection_goods.append(res_g)
    return JsonResponse(Res.success(res_selection_goods))


# 获取猜你喜欢商品
@csrf_exempt
def get_like_goods(request):
    like_goods = list()
    user = request.session.get("user")  # 获取用户登录信息
    if user is not None:
        # 用户登录情况下
        browse_num_sums = dict()  # 商品id:浏览次数
        browse_num_sum = 0  # 浏览总次数
        goods_ids = cache.get("browsing_history_" + str(user.get("id")))  # 获取浏览记录
        for goods_id in goods_ids.keys():  # 循环浏览过的商品
            browse_num = goods_ids.get(goods_id)  # 获取浏览次数
            goods = StoreGoods.objects.filter(id=goods_id).first()  # 获取具体商品
            if browse_num_sums.get(goods.type_id) is None:
                browse_num_sums[goods.type_id] = 0
            browse_num_sums[goods.type_id] += browse_num
            browse_num_sum += browse_num
        for menu_id in browse_num_sums.keys():
            num = browse_num_sums.get(menu_id)
            v = num / browse_num_sum  # 浏览概率
            _v = round(8 * v)
            goods_list = StoreGoods.objects.filter(type_id=menu_id).order_by("-id")[0:_v]
            for goods in goods_list:
                g = dict()
                g["id"] = goods.id
                g["goods_title"] = goods.goods_title
                g["goods_img"] = goods.goods_img
                g["goods_price"] = \
                    min(goods.goods_wph_price, goods.goods_jd_price, goods.goods_tm_price, goods.goods_tb_price,
                        goods.goods_sn_price)
                like_goods.append(g)
    else:
        # 用户未登录情况下
        print("user not login")
    return JsonResponse(Res.success(like_goods))


# 获取足迹商品信息
@csrf_exempt
def get_footprint(request):
    user = request.session.get("user")
    goods_ids = cache.get("browsing_history_" + str(user.get("id")))  # 获取浏览记录
    goods_list = list()
    for goods_id in goods_ids.keys():
        goods = StoreGoods.objects.filter(id=goods_id).first()
        g = dict()
        g["id"] = goods.id
        g["goods_title"] = goods.goods_title
        g["goods_img"] = goods.goods_img
        g["goods_price"] = \
            min(goods.goods_wph_price, goods.goods_jd_price, goods.goods_tm_price, goods.goods_tb_price,
                goods.goods_sn_price)
        goods_list.append(g)
    return JsonResponse(Res.success(goods_list[0:8]))


# 获取足迹商品信息
@csrf_exempt
def get_collection(request):
    user = request.session.get("user")
    goods_ids = cache.get("collection_" + str(user.get("id")))  # 获取收藏记录
    print(goods_ids)
    if goods_ids is None:
        return JsonResponse(Res.success(None))
    goods_list = list()
    for goods_id in goods_ids:
        goods = StoreGoods.objects.filter(id=goods_id).first()
        g = dict()
        g["id"] = goods.id
        g["goods_title"] = goods.goods_title
        g["goods_img"] = goods.goods_img
        g["goods_price"] = \
            min(goods.goods_wph_price, goods.goods_jd_price, goods.goods_tm_price, goods.goods_tb_price,
                goods.goods_sn_price)
        goods_list.append(g)
    return JsonResponse(Res.success(goods_list[0:8]))


@csrf_exempt
def add_collection(request):
    goods_id = request.POST.get("goods_id")
    user = request.session.get("user")
    collections = cache.get("collection_" + str(user.get("id")))
    if collections is None:
        collections = list()
    if goods_id not in collections:
        collections.append(goods_id)
    cache.set("collection_" + str(user.get("id")), collections, None)
    collections.append(goods_id)
    return JsonResponse(Res.success(None))
